<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/4/22  16:46
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Common\Model\BookModel;
use Admin\Common\Page;

class SearchBookController extends CashierBaseController {

    function index() {
        $map['is_del'] = 0;
        $keywords = I('get.keywords');
        if (!empty($keywords)) {
            $map_or['tm'] = $keywords;
            $map_or['sm'] = ['like' , "%{$keywords}%"];
            $map_or['_logic'] = 'OR';
            $map['_complex'] = $map_or;
        }
        $m = D('Book');
        $count = $m->where($map)->count();// 查询满足要求的总记录数
        $Page = new  Page($count , C('page.size'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $where['keywords'] = $keywords;
        foreach ($where as $key => $val) {
            $Page->parameter[ $key ] = urlencode($val);
        }
        $show = $Page->show();// 分页显示输出
        $this->assign('page' , $show);// 赋值分页输出
        $data = $m->where($map)->relation(TRUE)->order('book_id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->data = $data;
        $this->display();
    }

    //借阅用户记录
    function rentUser(){
        $book_id=I("post.book_id");
        $is_backed=I("post.is_backed");
        $page=I("post.page")==""?1:I("post.page");
        $map=[
            'book_id'=>$book_id,
            'is_backed'=>$is_backed
        ];
        if($is_backed=='2'){
            unset($map['is_backed']);
        }
        $c=M("View_order_has_book")->alias('a')
            ->where($map)
            ->join('card b on a.user_id=b.user_id','left')
            ->join('user c on a.user_id=c.user_id','left')
            ->group("a.user_id")
            ->field("max(card_id) as card_id")
            ->select();
        $c=sizeof($c);
        $error='查询图书，book_id:'.$book_id.',条件:'.json_encode($map).'当前总数:'.$c;
        $total=ceil($c/C('page.size'));
        $d=M("View_order_has_book")->alias('a')
            ->where($map)
            ->join('card b on a.user_id=b.user_id','left')
            ->join('user c on a.user_id=c.user_id','left')
            ->group("a.user_id")
            ->field("max(card_id) as card_id,b.child_name,b.father_name,b.father_phone,b.mother_name,b.mother_phone,c.nickname,c.openid,a.user_id")
            ->limit(($page-1)*C('page.size').",".C('page.size'))
            ->select();
        foreach($d as $k=>$v){
            $record=[];
            //查询借阅记录
            $record['quantity']=M("View_order_has_book")->where(['user_id'=>$v['user_id'],'type'=>'1','book_id'=>$book_id])->sum('quantity');
            $record['num_backed']=M("View_order_has_book")->where(['user_id'=>$v['user_id'],'type'=>'1','book_id'=>$book_id])->sum('num_backed');
            $record['num_broken']=M("View_order_has_book")->where(['user_id'=>$v['user_id'],'type'=>'1','book_id'=>$book_id])->sum('num_broken');
            if($record['quantity']==$record['num_broken']+$record['num_backed']){
                $record['rent_state']='已归还';
            }else{
                $record['rent_state']='在借';
            }
            $d[$k]['rent_record']=$record;
        }
        $j=[
            "data"=>$d,
            "total"=>$total,
            "page"=>$page,
        ];
        M('Log_error')->data(['error'=>$error])->add();
        echo json_encode($j);
    }
}